import BasicTool from '../BasicTool';

export default class DrawRectangle {

    constructor(viewer) {
        this.viewer = viewer;
        this._drawHandler = null;
        this.callback = null;
        this.floatingPoint = null;//标识点
        this._rectangle = null; //活动矩形
        this._rectangleLast = null; //最后一个矩形
        this._positions = [];  //活动点
        this._entities_point = [];  //脏数据
        this._entities_rectangle = [];  //脏数据
        this._rectangleData = null; //用于构造矩形数据
        this.basicTool = new BasicTool(this.viewer)
        this.callbackResult = {}
    }

    //返回最后图形
    getline() {
        return this._rectangleLast;
    }

    //返回矩形数据
    getData() {
        return this._rectangleData;
    }

    //加载
    loadRectangle(data) {
        var shape = this.viewer.entities.add({
            name: "rectangle",
            rectangle: {
                coordinates: Cesium.Rectangle.fromCartesianArray(data),
                material: Cesium.Color.RED.withAlpha(0.5)
            }
        });
        this._entities_rectangle.push(shape);
        return shape;
    }

    //开始创建
    activate(callback) {
        this.callback = callback;
        this._drawHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
        this._drawHandler.setInputAction((evt) => { //单机开始绘制
            //屏幕坐标转地形上坐标
            var cartesian = this.getCatesian3FromPX(evt.position);
            if (this._positions.length == 0) {
                this._positions.push(cartesian.clone());
                // this.floatingPoint = this.createPoint(cartesian);
                // this.createPoint(cartesian);// 绘制点
            }
            this._positions.push(cartesian);
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
        this._drawHandler.setInputAction((evt) => { //移动时绘制线
            if (this._positions.length < 2) return;
            var cartesian = this.getCatesian3FromPX(evt.endPosition);
            if (!Cesium.defined(this._rectangle)) {
                this._rectangle = this.createRectangle();
            }
            // this.floatingPoint.position.setValue(cartesian);
            if (this._rectangle) {
                this._positions.pop();
                this._positions.push(cartesian);
            }
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        this._drawHandler.setInputAction((evt) => {
            if (!this._rectangle) return;
            this._removeAllEvent()
            var cartesian = this.getCatesian3FromPX(evt.position);
            this._positions.pop();
            this._positions.push(cartesian);
            // this.createPoint(cartesian);// 绘制点
            this._rectangleData = this._positions.concat();
            this.viewer.entities.remove(this._rectangle); //移除
            this._rectangle = null;
            this._positions = [];
            // this.floatingPoint.position.setValue(cartesian);
            var rectangle = this.loadRectangle(this._rectangleData); //加载
            this._entities_rectangle.push(rectangle);
            this._rectangleLast = rectangle;
            if(typeof this.callback=="function"){
                // 转换为经纬度坐标
                var points = this._positions.map(r=>this.basicTool.cartesian2ArrayLonLat(r))
                // 回调
                this.callbackResult = {
                    type: "Rectangle",
                    state: "finish",
                    points: points,
                    postions: this._rectangleData,
                }
                this.callback(this.callbackResult);
            }
        }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
    }

    //创建点
    createPoint(cartesian) {
        var point = this.viewer.entities.add({
            position: cartesian,
            point: {
                pixelSize: 10,
                color: Cesium.Color.YELLOW,
            }
        });
        this._entities_point.push(point);
        return point;
    }

    //创建矩形
    createRectangle() {
        var shape = this.viewer.entities.add({
            name: "rectangle",
            rectangle: {
                coordinates: new Cesium.CallbackProperty(() => {
                    var obj = Cesium.Rectangle.fromCartesianArray(this._positions);
                    return obj;
                }, false),
                material: Cesium.Color.RED.withAlpha(0.5)
            }
        });
        this._entities_rectangle.push(shape);
        return shape;
    }

    /**
     * 移除所有鼠标事件
     * @private
     */
    _removeAllEvent() {
        this._drawHandler && (
            this._drawHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK),
            this._drawHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE),
            this._drawHandler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK),
            this._drawHandler.destroy(),
            this._drawHandler = null
        )
    }
 
    /**
     * 重置所有参数
     * @private
     */
    _resetParams() {
        for (var i = 0; i < this._entities_point.length; i++) {
            this.viewer.entities.remove(this._entities_point[i]);
        }
        for (var i = 0; i < this._entities_rectangle.length; i++) {
            this.viewer.entities.remove(this._entities_rectangle[i]);
        }
        this.floatingPoint = null;//标识点
        this._rectangle = null; //活动矩形
        this._rectangleLast = null; //最后一个矩形
        this._positions = [];  //活动点
        this._entities_point = [];  //脏数据
        this._entities_rectangle = [];  //脏数据
        this._rectangleData = null; //用于构造矩形数据
    }

    //清空实体对象
    clearAll() {
        this._removeAllEvent()
        this._resetParams()
    }

    getCatesian3FromPX(px) {
        var cartesian;
        var ray = this.viewer.camera.getPickRay(px);
        if (!ray) return null;
        cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
        return cartesian;
    }

}
